package yuhao.jdbc;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class Jdbc7 {
    public static void main(String[] args) throws Exception {

        //加载配置文件
        InputStream is = Jdbc7.class.getClassLoader().getResourceAsStream("druid.properties");


        Properties prop = new Properties();

        prop.load(is);

        //获取数据库连接对象
        DataSource ds = DruidDataSourceFactory.createDataSource(prop);

        Connection conn = ds.getConnection();

        //编写sql语句
        String sql = "SELECT * from account";

        PreparedStatement pst = conn.prepareStatement(sql);

        ResultSet rs = pst.executeQuery();

        while (rs.next()) {
            System.out.println(rs.getInt("id") + "," + rs.getString("name") + ","
                    + rs.getInt("money")
            );
        }
        rs.close();
        pst.close();
        conn.close();
    }


}

